package me.firstapp.service.weixin.transfer.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.module.weixin.transfer.TransferMember;
import me.firstapp.module.weixin.transfer.TransferMemberProduct;
import me.firstapp.module.weixin.transfer.TransferMemberProductItem;
import me.firstapp.repository.weixin.member.MemberRepository;
import me.firstapp.repository.weixin.transfer.TransferMemberProductItemRepository;
import me.firstapp.repository.weixin.transfer.TransferMemberProductRepository;
import me.firstapp.repository.weixin.transfer.TransferMemberRepository;
import me.firstapp.service.weixin.transfer.TransferMemberProductService;

@Service
public class TransferMemberProductServiceImpl implements TransferMemberProductService {

	@Autowired
	private TransferMemberRepository transferMemberRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private TransferMemberProductRepository transferMemberProductRepository;

	@Autowired
	private TransferMemberProductItemRepository transferMemberProductItemRepository;

	@Override
	public TransferMemberProduct executeAddTransferMemberProduct(String ownOpenId, BigDecimal amount, Integer counts) {
		Member member = memberRepository.findMemberByOpenId(ownOpenId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_MEMBER_NOT_EXIST);
		}
		List<TransferMember> transferMembers = transferMemberRepository.findTransferMembers(member.getId(),
				TransferMember.STATUS_NORMAL);
		if (counts > transferMembers.size()) {
			throw new ServiceException("error", "数量不合法");
		}
		TransferMemberProduct transferMemberProduct = new TransferMemberProduct();
		transferMemberProduct.setOwnOpenId(ownOpenId);
		transferMemberProduct.setOwnCardNo(member.getCardNo());
		transferMemberProduct.setOwnName(member.getName());
		transferMemberProduct.setImgUrl(member.getAvatar());
		transferMemberProduct.setName(member.getName() + "需要转让" + counts + "个用户");
		transferMemberProduct.setAmount(amount);
		transferMemberProduct.setCounts(counts);
		transferMemberProduct.setStatus(TransferMemberProduct.STATUS_ON);
		transferMemberProduct.setApplyTime(new Date());
		transferMemberProductRepository.save(transferMemberProduct);

		for (int i = 0; i < counts; i++) {
			TransferMember transferMember = transferMembers.get(i);
			TransferMemberProductItem transferMemberProductItem = new TransferMemberProductItem();
			transferMemberProductItem.setTransferMemberProductId(transferMemberProduct.getId());
			transferMemberProductItem.setMemberId(transferMember.getMemberId());
			transferMemberProductItemRepository.save(transferMemberProductItem);

			transferMember.setStatus(TransferMember.STATUS_CANCEL);
			transferMemberRepository.update(transferMember);
		}
		return transferMemberProduct;
	}

	@Override
	public List<TransferMemberProduct> findTransferMemberProducts(Integer pageNo, Integer pageSize) {
		return transferMemberProductRepository.findTransferMemberProducts(pageNo, pageSize);
	}

	@Override
	public TransferMemberProduct executeOffTransferMemberProduct(TransferMemberProduct transferMemberProduct) {
		if (null == transferMemberProduct) {
			return null;
		}
		if (TransferMemberProduct.STATUS_ON != transferMemberProduct.getStatus()) {
			return transferMemberProduct;
		}
		transferMemberProduct.setStatus(TransferMemberProduct.STATUS_OFF);
		transferMemberProduct.setOffTime(new Date());

		List<TransferMemberProductItem> transferMemberProductItems = transferMemberProductItemRepository
				.findByTransferMemberProductId(transferMemberProduct.getId());
		for (TransferMemberProductItem transferMemberProductItem : transferMemberProductItems) {
			Member member = memberRepository.findMemberByOpenId(transferMemberProduct.getOwnOpenId());
			TransferMember transferMember = transferMemberRepository
					.findTransferMember(transferMemberProductItem.getMemberId(), member.getId());
			if (null != transferMember) {
				transferMemberRepository.deleteById(transferMember.getId());
			}
		}
		return transferMemberProduct;
	}

	@Override
	public TransferMemberProduct findById(Long id) {
		return (TransferMemberProduct) transferMemberProductRepository.getById(id);
	}

	@Override
	public Page<TransferMemberProduct> findTransferMemberProductPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Date startTime, Date endTime) {
		return transferMemberProductRepository.findTransferMemberProductPage(pageNo, pageSize, searchParams, sortParam,
				sortType, startTime, endTime);
	}

}
